import { getCampusSelector } from '@/api/campus/campus'
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        name: undefined,
        campusName: undefined,
        type: undefined,
        brand: undefined,
        amount: undefined,
        time: undefined,
        status: undefined,
        managerId: undefined,
        reason: undefined,
        method: undefined,
        rAmount: undefined,
        receiptId: undefined
      },
      brandOptions: [],
      rules: {
        time: [
          {
            required: true,
            message: '请输入退款时间',
            trigger: 'change'
          }
        ],
        method: [
          {
            required: true,
            message: '请输入退款方式',
            trigger: 'change'
          }
        ],
        amount: [
          {
            required: true,
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的金额',
            trigger: 'blur'
          }
        ],
        rAmount: [
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的金额',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请输入退款类型',
            trigger: 'change'
          }
        ],
        reason: [
          {
            required: true,
            message: '请输入退款原因',
            trigger: 'blur'
          }
        ]
      },
      methodOptions: [{ fullName: '银行转账', id: '1' }, { fullName: '微信转账', id: '2' }, { fullName: '支付宝转账', id: '3' }, { fullName: '现金', id: '4' }],
      typeOptions: [{ fullName: '全额退款', id: '1' }, { fullName: '部分退款', id: '2' }],
      statusOptions: [{ fullName: '定金', id: '1' }, { fullName: '加盟费', id: '2' }, { fullName: '续费', id: '3' }, { fullName: '保证金', id: '4' }],
      // 新增：校区下拉选项与加载状态
      campusOptions: [],
      campusLoading: false
    }
  },
  computed: {},
  watch: {},
  created() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
    // 页面加载时预拉取校区下拉选项
    this.loadCampusOptions()
  },
  mounted() {},
  methods: {
    // 异步加载校区下拉选项
    async loadCampusOptions() {
      try {
        this.campusLoading = true
        const res = await getCampusSelector({ fields: 'id,fullName' })
        this.campusOptions = Array.isArray(res.data) ? res.data : []
      } catch (err) {
        console.error('加载校区下拉选项失败:', err)
        this.campusOptions = []
        this.$message && this.$message.error('校区选项加载失败')
      } finally {
        this.campusLoading = false
      }
    },
    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/finance/refunds/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    receiptInit(id, row) {
      this.dataForm = Object.assign(this.dataForm, {
        name: row.name,
        campusName: row.campusName,
        brand: row.brand,
        time: row.time,
        status: row.status,
        reason: row.reason,
        method: row.method
      })
      this.dataForm.receiptId = id
      this.dataForm.rAmount = row.amount
      this.visible = true
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (!this.dataForm.id) {
            request({
              url: `/api/finance/refunds`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  ;(this.visible = false), this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/finance/refunds/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
